package com.hb.trade.api.routine.controller.project.homepage;

import com.hb.core.pojo.RoleDataAuthEntity;
import com.hb.core.pojo.center.sys.dto.NewsCenterDTO;
import com.hb.core.pojo.project.homepage.dto.AnnouncementsDTO;
import com.hb.core.pojo.project.homepage.dto.StatisticUserParam;
import com.hb.core.pojo.project.homepage.vo.BidSectionStatisticVO;
import com.hb.core.pojo.project.section.dto.BidSectionCountDTO;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.api.routine.util.RoleDataAuthUtil;
import com.hb.trade.client.center.NewsCenterClient;
import com.hb.trade.client.project.AnnouncementsClient;
import com.hb.trade.client.project.BidOpeningHallClient;
import com.hb.trade.client.project.SectionClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 交易平台首页Api接口
 *
 * @author ljs
 * @date 2022-2-18 16:42:07
 *
 */
@Slf4j
@Version(1.0)
@Api(tags = {"平台首页"})
@RequestMapping("/{version}")
@RestController
public class ApiHomePageController {

    protected BidOpeningHallClient bidOpeningHallClient;
    protected AnnouncementsClient announcementsClient;
    protected NewsCenterClient newsCenterClient;
    protected SectionClient sectionClient;
    public ApiHomePageController(BidOpeningHallClient bidOpeningHallClient,
                                 AnnouncementsClient announcementsClient,
                                 NewsCenterClient newsCenterClient,
                                 SectionClient sectionClient){
        this.bidOpeningHallClient = bidOpeningHallClient;
        this.announcementsClient = announcementsClient;
        this.newsCenterClient = newsCenterClient;
        this.sectionClient = sectionClient;
    }

    @ApiOperation(value = "分页查询系统公告", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/home/announcementsList")
    public ResponseDto announcementsList(@RequestBody AnnouncementsDTO announcementsDTO) {
        Message result = announcementsClient.announcementsList(announcementsDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "查询系统公告详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "系统公告ID", dataTypeClass = Long.class)
    })
    @PostMapping("/home/announcementsDetail")
    public ResponseDto announcementsDetail(@RequestBody AnnouncementsDTO announcementsDTO) {
        return ResultUtil.msg(announcementsClient.announcementsDetail(announcementsDTO));
    }

    @ApiOperation(value = "新增或者修改系统公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "公告标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "id", value = "公告ID", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "公告正文", dataTypeClass = String.class)
    })
    @PostMapping("/home/AnnouncementsSave")
    public ResponseDto AnnouncementsSave(@RequestBody AnnouncementsDTO announcementsDTO) {
        return ResultUtil.msg(announcementsClient.AnnouncementsSave(announcementsDTO));
    }

    @ApiOperation(value = "删除系统公告", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告ID", dataTypeClass = String.class),
    })
    @PostMapping("/home/AnnouncementsRemove")
    public ResponseDto AnnouncementsRemove(@RequestBody AnnouncementsDTO announcementsDTO) {
        return ResultUtil.msg(announcementsClient.AnnouncementsRemove(announcementsDTO));
    }

    @ApiOperation(value = "查看今日开标", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openBidTime", value = "开标时间", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "roleSign", value = "当前用户角色首页使用：1、招标人/代理机构 2、投标人", dataTypeClass = Integer.class)
    })
    @PostMapping("/home/pageHomeTodayOpeningSection")
    public ResponseDto pageHomeTodayOpeningSection(@RequestBody BidSectionDTO bidSectionDTO) {
        Long userId = ShiroUtil.getUserId();
        bidSectionDTO.setCreateId(userId);
        if (bidSectionDTO.getPageSize()==null){
            bidSectionDTO.setPageSize(100);
        }
        return ResultUtil.msg(bidOpeningHallClient.pageHomeTodayOpeningSection(bidSectionDTO));
    }

    @ApiOperation(value = "待办任务", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsType", value = "消息类型", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "readFlag", value = "是否已读", dataTypeClass = Integer.class),
    })
    @PostMapping("/home/pageNewsCenter")
    public ResponseDto pageNewsCenter(@RequestBody NewsCenterDTO newsCenterDTO){
        Long userId = ShiroUtil.getUserId();
        Integer roleSign = ShiroUtil.getExtInfo().get("roleSign", Integer.class);

        newsCenterDTO.setReceiverId(userId);
        newsCenterDTO.setRoleSign(roleSign);
        Message result = newsCenterClient.pageNewsCenter(newsCenterDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "未递交投标文件标段", httpMethod = "POST")
    @ApiImplicitParams({
    })
    @PostMapping("/home/notSubmitSectionList")
    public ResponseDto notSubmitSectionList(@RequestBody BidSectionDTO bidSectionDTO){
        Long userId = ShiroUtil.getUserId();
        bidSectionDTO.setCreateId(userId);
        Message result = sectionClient.notSubmitSectionList(bidSectionDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "根据月份查询具有开标安排的日期", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "year", value = "年", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "roleSign", value = "当前用户角色首页使用：1、招标人/代理机构 2、投标人", dataTypeClass = Integer.class),
    })
    @PostMapping("/home/openDateList")
    public ResponseDto openDateList(@RequestBody BidSectionDTO bidSectionDTO){
        Long userId = ShiroUtil.getUserId();
        bidSectionDTO.setCreateId(userId);
        Message<List<BidSectionDTO>> result = sectionClient.openDateList(bidSectionDTO);
        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "首页项目数量统计", httpMethod = "POST")
    @PostMapping("/home/bidSectionStatistic")
    public ResponseDto bidSectionStatistic(@RequestBody BidSectionCountDTO bidSectionCountDTO) {
        Integer roleSign = ShiroUtil.getExtInfoInt("roleSign");
        Long userId = ShiroUtil.getUserId();

        StatisticUserParam userParam = new StatisticUserParam();
        userParam.setUserId(userId);
        userParam.setRoleSign(roleSign);

        // 设置数据权限, 添加数据权限条件
        RoleDataAuthEntity authEntity = RoleDataAuthUtil.buildDataAuth();
        userParam.setDataAuth(authEntity);

        Message<BidSectionStatisticVO> result = sectionClient.homepageSectionStatistic(userParam);
        return ResultUtil.msg(result);
    }

}
